package com.weiyi.weiman.quartz;

import com.alibaba.fastjson.JSON;
import com.weiyi.weiman.domain.Const.UserConst;
import com.weiyi.weiman.mapper.sys.SysUserLogMapper;
import com.weiyi.weiman.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sys.SysUserLog;

import java.util.List;
import java.util.Set;

/**
 * @Author zhang
 * @Date 2021/5/31 下午5:09
 * @Description
 * @Version 1.0
 **/
@Component
@Slf4j
public class LogTask {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SysUserLogMapper sysUserLogMapper;

    @Scheduled(cron = "0 30 3 * * ?")
    private void writeUserLog() {
        Set<String> allKeys = redisUtils.getAllKeys(UserConst.USER_LOG_PRE+"*");
        for (String key : allKeys) {
            List<String> logList = redisUtils.lRange(key, 0, -1);
            for (String log : logList) {
                SysUserLog userLog = JSON.parseObject(log, SysUserLog.class);
                sysUserLogMapper.insert(userLog);
            }
            redisUtils.delete(key);
        }

    }

}
